package com.zenithsun.news.newsinfo.controller;

import java.util.HashMap;
import java.util.List;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.syzg.common.controller.BaseController;
import com.syzg.common.exception.BaseException;
import com.syzg.common.util.SearchMap;
import com.zenithsun.common.security.csrf.CSRFTokenManager;
import com.zenithsun.common.security.sql.SqlInjectionManager;
import com.zenithsun.common.security.xss.XSSFilterManager;
import com.zenithsun.news.newscolumns.model.NewsColumns;
import com.zenithsun.news.newsinfo.service.NewsinfoService;

@Controller
public class FrontController  extends BaseController{
	static Logger log = LoggerFactory.getLogger(FrontController.class);

	@Resource
	private NewsinfoService newsinfoService;
	/**
	 * 新闻首页
	 * @param mv 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/index")
	public ModelAndView index(ModelAndView mv,String columnId) throws Exception {
		SqlInjectionManager.validateSqlParam(columnId);
		columnId=XSSFilterManager.xssEncode(columnId);
		mv.setViewName("/zenithsun/news/pc/index.jsp");
		try {
			List<HashMap<String, Object>> result = newsinfoService.listNewsinfo_carousel(4);
			mv.addObject("carouselNews",result);
			List<HashMap<String, Object>> tops = newsinfoService.listNewsinfo_top(10);
			
			mv.addObject("topNews",tops);
			SearchMap searchMap = SearchMap.forClass(NewsColumns.class);
			searchMap.eq("a.parentId", "0");
			searchMap.addOrder("orderNo", (short)1);
			@SuppressWarnings("unchecked")
			List<NewsColumns> lstNewsColumns = newsinfoService.findObjects(searchMap);
			mv.addObject("newsColumns", lstNewsColumns);
			if(columnId == null || columnId.equals(""))columnId = "";//默认 首页
			List<HashMap<String, Object>> newsByColumn = newsinfoService.listNewsinfo_columns(10, columnId);
			mv.addObject("newsByColumn", newsByColumn);
			mv.addObject("columnId", columnId);
			
			List<HashMap<String, Object>> specialNews = newsinfoService.getSpecialNewsUpToDate(1);
			mv.addObject("specialNews", specialNews.get(0));
			
			//CSRF_TOKEN_FOR_FORM_PARAM_NAME
			mv.addObject("CSRF_TOKEN_FOR_FORM_PARAM_NAME",CSRFTokenManager.CSRF_TOKEN_FOR_FORM_PARAM_NAME);
		} catch (Exception e) {
			log.error(BaseException.addExceptionLog(e));
		}
		return mv;
	}
}
